﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Profile;
using System.Data;
using System.Data.SqlClient;
using System.Text;

using EHTSaleManager.Code;
using EHTSaleManager.Code.BLL;
using EHTSaleManager.Code.Utility;
using EHTSaleManager.Code.Entity;

namespace EHTSaleManager
{
    public partial class IndexMaster : System.Web.UI.MasterPage
    {
        private DataTable dtCategorys = new DataTable();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                dtCategorys = CategoryProvider.GetCategorysForFrontEnd().Tables[0];

                CreateMenuCategory();
                Createbanner();
                Createbannerleft();

                rpNewsTop.DataSource = NewsProvider.GetTopNews();
                rpNewsTop.DataBind();

                dtlCategoryNews.DataSource = CategoryProvider.GetTopCategorys();
                dtlCategoryNews.DataBind();

                BindData();

                if (ShoppingCart.Instance.GetSubTotal() == 0)
                {
                    ltrTotal.Text = "";
                }
                else
                {
                    ltrTotal.Text = "Totalpris:" + ShoppingCart.Instance.GetSubTotal().ToString("C");
                }

                if (Session["Username"] != null)
                {
                    ltrHello.Text = "Velkommen:";
                    ltrUser.Text = HttpContext.Current.Session["Username"].ToString();
                    ltrLogout.Text = "Loggut";
                }
            }
        }

        /// <summary>
        /// Author : toanvt
        /// CreateDate : 31/01/2012
        /// Description : Phương thức tạo menu
        /// </summary>
        private void CreateMenuCategory()
        {
            lrtCategoryMenu.Text = GetSubCategorys(0);
        }

        /// <summary>
        /// Author : toanvt
        /// CreateDate : 31/01/2012
        /// Description : Phương thức đệ quy tạo menu
        /// </summary>
        private String GetSubCategorys(Int32 categoryID)
        {
            StringBuilder sb = new StringBuilder();
            DataRow[] dataRowsSubCateogrys = dtCategorys.Select("ParentID = " + categoryID.ToString());

            Int32 amountOfSubCategory = dataRowsSubCateogrys.Count();

            //Điểm dừng đệ quy
            if (amountOfSubCategory == 0)
                return "";

            sb.Append("<ul>");

            for (Int32 i = 0; i < amountOfSubCategory; ++i)
            {
                sb.Append("<li>");
                sb.Append("<a href='IndexListProduct.aspx?CategoryID=" + dataRowsSubCateogrys[i]["CategoryID"].ToString() + "'>");
                sb.Append(dataRowsSubCateogrys[i]["CategoryName"].ToString());
                sb.Append("</a>");
                sb.Append(GetSubCategorys(Int32.Parse(dataRowsSubCateogrys[i]["CategoryID"].ToString())));
                sb.Append("</li>");
            }

            sb.Append("</ul>");

            return sb.ToString();
        }

        protected void BindData()
        {
            // Let's give the data to the GridView and let it work!
            // The GridView will take our cart items one by one and use the properties
            // that we declared as column names (DataFields)
            gvShoppingCartSum.DataSource = ShoppingCart.Instance.Items;
            gvShoppingCartSum.DataBind();

        }


        protected void Imgsearch_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("IndexResult.aspx?search=" + txtsearch.Text);
        }

        protected void ImagLogin_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("IndexLogin.aspx");
        }

        protected void ImagNewCustomer_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("IndexRegister.aspx");
        }
        public void Createbannerleft()
        {
            Int32 PhanVungID = 2;
            DataSet ds = QuangCaoProvider.GetPhanVungQuangCao(PhanVungID);

            if (ds.Tables[0].Rows.Count > 0)
            {
                this.ltrBannerQCLEFT.Text = ds.Tables[0].Rows[0]["Noidung"].ToString();
            }
            else
            {
                this.ltrBannerQCLEFT.Text = "";
            }
        }
        public void Createbanner()
        {
            
            Int32 PhanVungID = 3;
            DataSet ds = QuangCaoProvider.GetPhanVungQuangCao(PhanVungID);

            if (ds.Tables[0].Rows.Count > 0)
            {
                this.ltrBannerQCRight.Text = ds.Tables[0].Rows[0]["NoiDung"].ToString();
            }
            else
            {
                this.ltrBannerQCRight.Text = "";
            }
        }
        protected void rptCategory_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            //load menu con theo menu category

            DataRowView dr = (DataRowView)e.Item.DataItem;//neu datasource la datatable
            Repeater rptmenu = (Repeater)e.Item.FindControl("rptMenu");//tim rptMenu con trong rptCategory

            if (rptmenu != null)
            {
                DataSet dsCategory = CategoryProvider.GetCategorysByParentID(Int32.Parse(dr["CategoryID"].ToString()));//doc danh sach menu con theo id menu cha

                if (dsCategory != null)
                {
                    //neu co menu con, bind xuong rptmenu
                    rptmenu.DataSource = dsCategory;
                    rptmenu.DataBind();
                }
            }
        }
    }
}